package com.example.check_oa.model.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.check_oa.model.mapper.BusinessTripApMapper;
import com.example.check_oa.model.mapper.HolidayApMapper;
import com.example.check_oa.model.mapper.MouthAtMapper;
import com.example.comment_oa.pojos.attendance.BusinessTripAp;
import com.example.comment_oa.pojos.attendance.HolidayAp;
import com.example.comment_oa.pojos.attendance.OvertimeAp;
import com.example.check_oa.model.service.OvertimeApService;
import com.example.check_oa.model.mapper.OvertimeApMapper;
import com.example.check_oa.utils.GetAntten;
import com.example.comment_oa.pojos.MouthAt;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
* @author cjl
* @description 针对表【overtime_ap】的数据库操作Service实现
* @createDate 2022-10-27 18:09:42
*/
@Service
public class OvertimeApServiceImpl extends ServiceImpl<OvertimeApMapper, OvertimeAp>
    implements OvertimeApService{
    private String url="D:\\study\\gitee\\my_springcloud_project01cangku\\capacity\\src\\views\\capacity\\cjl\\img\\overTime";

    @Resource
    OvertimeApMapper overtimeApMapper;
    @Resource
    BusinessTripApMapper businessTripApMapper;
    @Resource
    HolidayApMapper holidayApMapper;
    @Resource
    MouthAtMapper mouthAtMapper;

    //新增加班申请
    @Override
    public int add(OvertimeAp overtimeAp, MultipartFile file) {
        overtimeAp.setOtStartTime(overtimeAp.getTime().get(0));
        overtimeAp.setOtEndTime(overtimeAp.getTime().get(1));
        //上月已归档
        QueryWrapper<MouthAt> wrapper8=new QueryWrapper<>();
        Date rcTime = overtimeAp.getOtStartTime();
        wrapper8.like("mouth",DateUtil.formatDate(rcTime).substring(0,7));
        List<MouthAt> mouthAts = mouthAtMapper.selectList(wrapper8);
        if(!mouthAts.isEmpty()){
            return 6;
        }
        //今年工作日集合
        List<String> yearWorkDay = GetAntten.getYearWorkDay();
        //传入的加班集合
        List<String> betweenDate = GetAntten.getBetweenDate(DateUtil.formatDate(overtimeAp.getOtStartTime()), DateUtil.formatDate(overtimeAp.getOtEndTime()));

        QueryWrapper<OvertimeAp> wrapper=new QueryWrapper<>();
        wrapper.eq("user_ot",overtimeAp.getUserOt()).and(i->i.eq("ot_state",1).or().eq("ot_state",0));
        List<OvertimeAp> overtimeAps = overtimeApMapper.selectList(wrapper);
        List<String> betweenDate1=new ArrayList<>();
        overtimeAps.forEach(e->{
            betweenDate1.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getOtStartTime()),DateUtil.formatDate(e.getOtEndTime())));
        });
        if(GetAntten.intersectList2(yearWorkDay,betweenDate).size()>0){
            //存在工作日（工作日不加班）
            return 0;
        }
        if(GetAntten.intersectList2(betweenDate1,betweenDate).size()>0){
            //重复加班，或正在审批
            return 2;
        }
        //出差
        QueryWrapper<BusinessTripAp> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_bt",overtimeAp.getUserOt()).and(i->i.eq("bt_ap_state",1).or().eq("bt_ap_state",0));
        List<BusinessTripAp> businessTripAps = businessTripApMapper.selectList(wrapper1);
        List<String> betweenDate2=new ArrayList<>();
        businessTripAps.forEach(e->{
             betweenDate2.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getBtStartTime()), DateUtil.formatDate(e.getBtEndTime())));
        });
        if(GetAntten.intersectList2(betweenDate2,betweenDate).size()>0){
            //加班日期中存在出差日期
            return 3;
        }
        //请假
        QueryWrapper<HolidayAp> wrapper2=new QueryWrapper<>();
        wrapper2.eq("user_ho",overtimeAp.getUserOt());
        wrapper2.eq("ho_state",1).or().eq("ho_state",0);
        List<HolidayAp> holidayAps = holidayApMapper.selectList(wrapper2);
        List<String> betweenDate3=new ArrayList<>();
        holidayAps.forEach(e->{
            betweenDate3.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getHoApStartTime()), DateUtil.formatDate(e.getHoApEndTime())));
        });
        if(GetAntten.intersectList2(betweenDate3,betweenDate).size()>0){
            //加班日期中存在请假日期
            return 4;
        }
        //所有节假日集合
        List<String> allTx = Arrays.asList(GetAntten.holiday1);
        if(GetAntten.intersectList2(allTx,betweenDate).size()==betweenDate.size()){
            //节假日加班
            overtimeAp.setOvertimeTypeId(2);
        }else{
            //双休加班
            overtimeAp.setOvertimeTypeId(1);
        }
        if(file!=null){
            //获得文件的名称
            String fileName = file.getOriginalFilename();
            fileName = file.getOriginalFilename();
            //取后缀名
            String ext = FilenameUtils.getExtension(fileName);
            //创建一个新的文件名
            fileName = UUID.randomUUID().toString() +"."+ ext;
            File f =  new File(url,fileName);
            try {
                file.transferTo(f);//进行文件转移
            } catch (IOException e) {
                e.printStackTrace();
            }
            String filePath = "capacity/cjl/img/overTime"+fileName;
            overtimeAp.setOtImg(filePath);
        }
        overtimeAp.setOtTime((double) betweenDate.size());
        return overtimeApMapper.insert(overtimeAp);
    }

    @Override
    public int yesOv(Integer ovId, String yesUser) {
        UpdateWrapper<OvertimeAp> wrapper=new UpdateWrapper<>();
        wrapper.eq("ot_water",ovId);
        OvertimeAp overtimeAp=new OvertimeAp();
        overtimeAp.setOtApYesTime(new Date());
        overtimeAp.setOtYesUser(yesUser);
        overtimeAp.setOtState(1);
        return overtimeApMapper.update(overtimeAp,wrapper);
    }

    @Override
    public int noOv(Integer ovId, String yesUser) {
        UpdateWrapper<OvertimeAp> wrapper=new UpdateWrapper<>();
        wrapper.eq("ot_water",ovId);
        OvertimeAp overtimeAp=new OvertimeAp();
        overtimeAp.setOtApYesTime(new Date());
        overtimeAp.setOtYesUser(yesUser);
        overtimeAp.setOtState(2);
        return overtimeApMapper.update(overtimeAp,wrapper);
    }
}




